//
//  HomeViewController.m
//  projectBase
//
//  Created by 洪梦萍 on 2017/10/18.
//  Copyright © 2017年 洪梦萍. All rights reserved.
//

#import "HomeViewController.h"
#import "Home_one_TableViewCell.h"
#import "Home_two_TableViewCell.h"
#import "SGFocusImageFrame.h"
#import "ZuoyeDetailViewController.h"
#import "BRPickerView.h"
#import "SizeHeader.h"
typedef NS_ENUM(NSInteger, UI_tag)
{
    xiaoxiBtn_tag = 1000000,
    buzhiBtn_tag,
    banjiBtn_tag,
    zuoyeBtn_tag,
    yibuzhiBtn_tag,
    banjiLab_tag,
    zuoyeLab_tag,
    yibuzhiLab_tag,
    sanchuBtn_tag,
    zuoyeImg1_tag,
    zuoyeImg2_tag,
    zuoyeImg3_tag,
    SGFocusImageFrame_tag,
    moreBtn_tag,
};

@interface HomeViewController ()<SGFocusImageFrameDelegate,UITableViewDelegate,UITableViewDataSource>
@property(strong,nonatomic)NSMutableArray * selectFont;
@property(strong,nonatomic)NSMutableArray * tubiaoAry;
@property(strong,nonatomic)NSMutableArray * titleAry;
@property(strong,nonatomic)UITableView * tableView;
@end

@implementation HomeViewController
-(void)initData
{
    _selectFont = [[NSMutableArray alloc]initWithObjects:@"四年级一班",@"线上作业",@"已布置", nil];
    _tubiaoAry = [[NSMutableArray alloc]initWithObjects:[UIImage imageNamed:@"sy_26"],[UIImage imageNamed:@"sy_24"],[UIImage imageNamed:@"sy_25"], nil];
    _titleAry = [[NSMutableArray alloc]initWithObjects:@"语文二年级下学期半期考",@"语文二年级下学期半期考",@"语文二年级下学期半期考", nil];
}
-(void)initUI
{
    [super initUI];
    [self setTopTitle:@"首页"];
    [self setTopTitleColor:[UIColor whiteColor]];
    [self initData];

    UIButton * xiaoxiBtn = [self createBtn:CGRectMake(ScreenWidth*18/375, ScreenHeight*30/667, ScreenWidth*26/375, ScreenHeight*29/667) title:@"" iconImage:[UIImage imageNamed:@"message"]backgroundImage:nil tag:xiaoxiBtn_tag textColor:nil];
    [self.view addSubview:xiaoxiBtn];
    UIButton * buzhiBtn = [self createBtn:CGRectMake(ScreenWidth*338/375, xiaoxiBtn.frame.origin.y, 26, 29) title:@"" iconImage:[UIImage imageNamed:@"fabu"] backgroundImage:nil tag:buzhiBtn_tag textColor:nil];
    [self.view addSubview:buzhiBtn];
    NSLog(@"%f,%f",ScreenWidth,ScreenHeight);
    //http://pic.qiantucdn.com/58pic/17/34/43/44i58PICXSp_1024.jpg!/fw/780/watermark/url/L3dhdGVybWFyay12MS4zLnBuZw==/align/center
    NSMutableArray * banner = [NSMutableArray array];
    for (int i = 0; i < 3; i++)
    {
        SGFocusImageItem * item = [[SGFocusImageItem alloc]init];
        item.imageUrl = [NSURL URLWithString:@"http://pic.qiantucdn.com/58pic/17/34/43/44i58PICXSp_1024.jpg!/fw/780/watermark/url/L3dhdGVybWFyay12MS4zLnBuZw==/align/center"];
        [banner addObject:item];
    }
    SGFocusImageFrame *imageFrame = [[SGFocusImageFrame alloc] initWithFrame:CGRectMake(0, TopAndSystemHeight, ScreenWidth, ScreenHeight*(265+59)/1442.0)delegate:self focusImageItemsArrray:banner];
    [self.view addSubview:imageFrame];
    
    UIView * tongzhiView = [[UIView alloc]initWithFrame:CGRectMake(0, imageFrame.frame.size.height+TopAndSystemHeight-ScreenHeight*59/1350, ScreenWidth, ScreenHeight*59/1350)];
    tongzhiView.backgroundColor = UIColorRGB(247);
    //透明度
    tongzhiView.alpha = 0.5f;
    [self.view addSubview:tongzhiView];
    
    UILabel * tongzhiLab = [self createLabel:CGRectMake(ScreenWidth*49/1350, ScreenHeight*15/1350, ScreenWidth*268/375, ScreenHeight*24/1350) title:@"【通知】学校春游，明天大家早上8点到学校集合" fontSize:14 textColor:MainColor];
    [tongzhiView addSubview:tongzhiLab];
    UIButton * moreBtn = [self createBtn:CGRectMake(ScreenWidth*335/375, tongzhiLab.frame.origin.y, ScreenWidth*30/375, ScreenHeight*13/667) title:@"更多" iconImage:nil backgroundImage:nil tag:moreBtn_tag textColor:UIColorRGB(172)];
    [tongzhiView addSubview:moreBtn];
    UIView * selectView = [[UIView alloc]initWithFrame:CGRectMake(0,imageFrame.frame.origin.y+imageFrame.frame.size.height , ScreenWidth, ScreenHeight*84.0/1442)];
    selectView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:selectView];
    
    for (int i = 0; i <3; i++)
    {
         UIButton * selectBtn = [self createBtn:CGRectMake(ScreenWidth*25/750+i*(ScreenWidth*178/750)+i*(ScreenWidth*84/750), ScreenHeight*12/1350.0, ScreenWidth*178/750, ScreenHeight*54/1250.0) title:@"" iconImage:[UIImage imageNamed:@"Rounded rectangle-539-4"] backgroundImage:nil tag:banjiBtn_tag+i textColor:nil];
        UIButton * TriangleBtn = [self createBtn:CGRectMake((ScreenWidth*145/750.0), 15, ScreenWidth*13/750.0, ScreenHeight*9/1350.0) title:@"" iconImage:[UIImage imageNamed:@"三角图标-下"] backgroundImage:nil tag:0 textColor:nil];
        [selectView addSubview:selectBtn];
        [selectBtn addSubview:TriangleBtn];
        UILabel * selectLab = [self createLabel:CGRectMake(ScreenWidth*25/750+i*(ScreenWidth*178/750)+i*(ScreenWidth*84/750), ScreenHeight*12/1350.0, ScreenWidth*178/750-ScreenWidth*13/750.0, ScreenHeight*54/1250.0) title:_selectFont[i] fontSize:15 textColor:[UIColor blackColor]];
        selectLab.tag = banjiLab_tag+i;
        selectLab.textAlignment = NSTextAlignmentCenter;
        [selectView addSubview:selectLab];
    }
    _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, imageFrame.frame.size.height+TopAndSystemHeight+selectView.frame.size.height+ScreenHeight*9.0/667, ScreenWidth, ScreenHeight-ScreenHeight*(265+59)/1442.0-TopAndSystemHeight-ScreenHeight*84.0/1442-ScreenHeight*9.0/750)];
    _tableView.backgroundColor = [UIColor whiteColor];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [_tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    [self.view addSubview:_tableView];

    
    
}
-(void)btnAction:(id)sender
{
    UIButton * btn = (UIButton *)sender;
    double tag = [btn tag];
    if (tag == xiaoxiBtn_tag)
    {
        NSLog(@"消息");
    }
    if (tag == banjiBtn_tag)
    {
        [BRStringPickerView showStringPickerWithTitle:@"请选择班级" dataSource:@[@"四年级一班", @"四年级二班"] defaultSelValue:@"四年级一班" isAutoSelect:YES resultBlock:^(id selectValue) {
            UILabel * selectLab = [self.view viewWithTag:banjiLab_tag];
            selectLab.text = selectValue;
            [_tableView reloadData];
        }];


    }
    if (tag == zuoyeBtn_tag)
    {
        [BRStringPickerView showStringPickerWithTitle:@"请选择类型" dataSource:@[@"试卷录入", @"线上作业", @"线下作业"] defaultSelValue:@"试卷录入" isAutoSelect:YES resultBlock:^(id selectValue) {
            UILabel * selectLab = [self.view viewWithTag:zuoyeLab_tag];
            selectLab.text = selectValue;
            [_tableView reloadData];

        }];

    }
    if (tag == yibuzhiBtn_tag)
    {
        [BRStringPickerView showStringPickerWithTitle:@"请选择" dataSource:@[@"已布置", @"已过期"] defaultSelValue:@"已布置" isAutoSelect:YES resultBlock:^(id selectValue) {
            UILabel * selectLab = [self.view viewWithTag:yibuzhiLab_tag];
            selectLab.text = selectValue;
            [_tableView reloadData];

        }];

    }
}

#pragma mark - SGFocusImageFrameDelegate
-(void)foucusImageFrame:(SGFocusImageFrame *)imageFrame didSelectItem:(SGFocusImageItem *)item
{
    NSLog(@"%@",item.title);
}
#pragma mark - TableView
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    UILabel * lab1 = [self.view viewWithTag:zuoyeLab_tag];
    UILabel * lab2 = [self.view viewWithTag:yibuzhiLab_tag];
    if ([lab1.text isEqualToString:@"线下作业"]&&[lab2.text isEqualToString:@"已布置"])
    {
        return 3;
        
    }
    if ([lab1.text isEqualToString:@"线上作业"]&&[lab2.text isEqualToString:@"已布置"])
    {
        return 3;
    }
    return 1;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
//1350 750
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UILabel * lab1 = [self.view viewWithTag:zuoyeLab_tag];
    UILabel * lab2 = [self.view viewWithTag:yibuzhiLab_tag];
    if ([lab1.text isEqualToString:@"线上作业"]&&[lab2.text isEqualToString:@"已布置"])
    {
        Home_one_TableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"TABLEVIEW"];
        if (cell == nil)
        {
            cell = [[[Home_one_TableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"TABLEVIEW"]initWithFrame:CGRectMake(0, ScreenHeight*(265+59)/1442.0+TopAndSystemHeight+ScreenHeight*84.0/1442+ScreenHeight*9.0/667, ScreenWidth, ScreenHeight-ScreenHeight*(265+59)/1442.0-TopAndSystemHeight-ScreenHeight*84.0/1442-ScreenHeight*9.0/750)];

            cell.titleLab.text = _titleAry[indexPath.row];
            cell.sanchuBtn.tag = sanchuBtn_tag;
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;

        return cell;
    }
    if ([lab1.text isEqualToString:@"线下作业"]&&[lab2.text isEqualToString:@"已布置"])
    {
        Home_two_TableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"TABLEVIEW2"];
        if (cell == nil)
        {
            cell = [[[Home_two_TableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"TABLEVIEW2"]initWithFrame:CGRectMake(0, ScreenHeight*(265+59)/1442.0+TopAndSystemHeight+ScreenHeight*84.0/1442+ScreenHeight*9.0/667, ScreenWidth, ScreenHeight-ScreenHeight*(265+59)/1442.0-TopAndSystemHeight-ScreenHeight*84.0/1442-ScreenHeight*9.0/750)];
                
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;

        return cell;
    }
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"table"];
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"table"];
        UIImageView * noHomeworkImage = [[UIImageView alloc]initWithFrame:CGRectMake(ScreenWidth/2.0-(ScreenWidth*180.0/750)/2.0, ScreenHeight*75/1350, ScreenWidth*180.0/750, ScreenHeight*174/1350)];
         noHomeworkImage.image = [UIImage imageNamed:@"椭圆-2595-拷贝-2"];
         [cell addSubview:noHomeworkImage];
         
         UILabel * noHomeworkLab = [self createLabel:CGRectMake(0, noHomeworkImage.frame.origin.y+noHomeworkImage.frame.size.height+ScreenHeight*31/735, ScreenWidth, 30) title:@"" fontSize:16 textColor:UIColorRGB(217)];
         [noHomeworkLab setText:@"还没有布置作业哟~"];
         noHomeworkLab.textAlignment = NSTextAlignmentCenter;
         [cell addSubview:noHomeworkLab];
        
    
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;

    return cell;

}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UILabel * lab1 = [self.view viewWithTag:zuoyeLab_tag];
    UILabel * lab2 = [self.view viewWithTag:yibuzhiLab_tag];
    if ([lab1.text isEqualToString:@"线下作业"]&&[lab2.text isEqualToString:@"已布置"])
    {
        return ScreenHeight*246/1350;

    }
    if ([lab1.text isEqualToString:@"线上作业"]&&[lab2.text isEqualToString:@"已布置"])
    {
        return ScreenHeight*177/1350;
    }
    return ScreenHeight-ScreenHeight*(265+59)/1442.0-TopAndSystemHeight-ScreenHeight*84.0/1442-ScreenHeight*9.0/750;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    ZuoyeDetailViewController * zy = [[ZuoyeDetailViewController alloc]init];
    [[ViewManager shareInstance].NavigationController pushViewController:zy animated:YES];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
